package cn.edu.scau.cmi.xuminbang.client.classicsDesignPattern;

import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.xuminbang.singleton.Chairman;
import cn.edu.scau.cmi.xuminbang.singleton.Marshal;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;


public class SingletonClient extends Application implements Initializable{
    @FXML private Button creatInstance;
    @FXML private TextField name;
    @FXML private javafx.scene.control.Label show1;
    @FXML private javafx.scene.control.Label attention;
    @FXML private TextField name1;
    @FXML private Button creatInstances;
    @FXML private javafx.scene.control.Label show2;
    @FXML private TextArea instances;
    int flag = 1;
    ArrayList<String> names = new ArrayList<>();

	@Override
    public void start(Stage primaryStage) throws Exception {
		
		Stage stage = new Stage();
		stage = primaryStage;
        try{
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("/cn/edu/scau/cmi/xuminbang/fxml/SingletonClient.fxml"));
            Parent root = (Parent)loader.load();
            Scene scene = new Scene(root);
            stage.setTitle("Singleton");
            stage.setScene(scene);
            stage.show();
        }catch (Exception e){
            e.printStackTrace();
        }
        
	}
	public static void main(String[] args) {
        launch(args);
    }
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		
	}
	public void getInstance() {
		 String text ="init";
		  try {
			text = name.getText();			
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		Chairman instance = Chairman.getInstance(text);
		show1.setText(Chairman.getName()+instance.toString()+"\n");
		if(Chairman.getName().equals(text)) {
			attention.setText("结果："+text+"实例创建成功或已存在");
		}else {
			attention.setText("结果："+text+"实例创建失败");
		}  
	}
	public void getInstances() {
		String tempOfInstance = "init";
		try {
			tempOfInstance = name1.getText();
		}
		catch (Exception e) {
			// TODO: handle exception
		}
		for(int i=0;i<names.size();i++) {
			if(tempOfInstance.equals(names.get(i))) {
				flag = 0;
				show2.setText("实体创建失败！实体已存在");
			}
			if(names.size()==10) {
				flag = 0;
				show2.setText("实体创建失败！实体数需<10");
			}
		}
		if(flag==1&&(!tempOfInstance.equals("init"))) {
			names.add(tempOfInstance);
			Marshal instanceOfMarshal = Marshal.getInstance(tempOfInstance);
			show2.setText("实体创建成功！");
			instances.appendText(tempOfInstance+instanceOfMarshal.toString()+"\n");
		}
		flag = 1;
	}

}
